home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / SpinCursor.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  3.6 KB  |  125 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1992-93 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Programmer: Kent Sandvik
  5.   Date: 12/14/92
  6.   Revision comments are at the end of this file.
  7.   ---
  8.   TSpinCursor is a simple cursor spinning class.
  9.   SpinCursor.cp contains the TSpinCursor member functions.
  10.   _________________________________________________________________________________________________________ */
  11.  
  12. #ifndef _SPINCURSOR_
  13. #include "SpinCursor.h"
  14. #endif
  15.  
  16.  
  17. //    CONSTRUCTORS AND DESTRUCTORS
  18. #pragma segment SpinCursor
  19. TSpinCursor::TSpinCursor(short acurID,
  20.                          short spinDirection,
  21.                          short spinTicks)
  22. // Default constructor, bind 'acur' and class together by specifying used 'acur' resource.
  23. {
  24.     fCursorHandle = NULL;
  25.     fSpinDirection = spinDirection;
  26.     fTickCounter = 0;
  27.     fSpinTicks = spinTicks;                        // how many ticks between spins
  28.  
  29.     fCursorList = (AnimationCursRec * *)::GetResource('acur', acurID);
  30.     fError = ::ResError();
  31.     VASSERT(fError == noErr, ("Problems with GetResource = %d", fError));
  32.  
  33.     if (fCursorList != NULL)
  34.     {
  35.         ::HNoPurge((Handle)fCursorList);        // don't purge this resource during use!
  36.  
  37.         for (fIndex = 0; fIndex < (**fCursorList).information.count; fIndex++)
  38.         {
  39.             fCursorID = (short)::HiWord((long)(**fCursorList).nCursors[fIndex]);
  40.             fCursorHandle = ::GetCursor(fCursorID);
  41.             ASSERT(fCursorHandle != NULL, "\pGetCursor returned NULL handle");
  42.  
  43.             (**fCursorList).nCursors[fIndex] = fCursorHandle;
  44.  
  45.             if (fCursorHandle != NULL)
  46.                 ::HNoPurge((Handle)fCursorHandle);// make every CURS handle non-purgeable
  47.         }
  48.     }
  49. }
  50.  
  51.  
  52. #pragma segment SpinCursor
  53. TSpinCursor::~TSpinCursor()
  54. // Clean up after the spinning, handles and such…
  55. {
  56.     CursHandle tempHandle;
  57.     short index;
  58.  
  59.     if (fCursorList != NULL)                    // sanity check
  60.     {
  61.         for (index = 0; index < (**fCursorList).information.count; index++)
  62.         {
  63.             tempHandle = (**fCursorList).nCursors[index];
  64.             if (tempHandle != NULL)
  65.                 ::HPurge((Handle)tempHandle);
  66.         }
  67.         ::ReleaseResource((Handle)fCursorList);    // don't dispose resources
  68.         fError = ResError();
  69.         VASSERT(fError == noErr, ("Problems with ReleaseResource = %d", fError));
  70.     }
  71. }
  72.  
  73.  
  74. // MAIN INTERFACE
  75.  
  76. #pragma segment SpinCursor
  77. void TSpinCursor::Spin()
  78. // Spin the cursor once (next frame) into earlier defined direction.
  79. {
  80.     long ticks;                                    // temp value
  81.     short count;                                // # of animated cursors
  82.     short frameNum;                                // # of frame
  83.  
  84.     if (fCursorList != NULL)                    // sanity check
  85.     {
  86.         ticks = ::TickCount();                    // get the stamp
  87.  
  88.         if ((ticks - fTickCounter) > fSpinTicks)// enough ticks between spins?
  89.         // …then spin, change new cursor, increase the frame count (next CURS),
  90.         // and save the new tick stamp.
  91.         {
  92.             count = (**fCursorList).information.count;
  93.             frameNum = (**fCursorList).frame % count;
  94.             fCursorHandle = (**fCursorList).nCursors[frameNum];
  95.  
  96.             if (fCursorHandle != NULL)            // sanity check
  97.             {
  98.                 ::HLock((Handle)fCursorHandle);    // lock the resource
  99.                 ::SetCursor(&(**fCursorHandle));// set the new CURS
  100.                 ::HUnlock((Handle)fCursorHandle);// unlock it
  101.             }
  102.             (**fCursorList).frame = (frameNum + count + fSpinDirection) % count;
  103.         }
  104.     }
  105. }
  106.  
  107. #pragma segment SpinCursor
  108. void TSpinCursor::SetSpinDirection(EDirection direction)
  109. // Set direction of spin, -1 backwards, 1 = forwards…
  110. {
  111.     fSpinDirection = direction;
  112. }
  113.  
  114.  
  115. // _________________________________________________________________________________________________________ //
  116.  
  117.  
  118. /*    Change History (most recent last):
  119.   No        Init.    Date        Comment
  120.   1            khs        12/14/92    New file
  121.   2            khs        1/3/93        Cleanup
  122. */
  123.  
  124.  
  125.